define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/brokerage_log/index' + location.search,
                    // add_url: 'user/brokerage_log/add',
                    // edit_url: 'user/brokerage_log/edit',
                    del_url: 'user/brokerage_log/del',
                    multi_url: 'user/brokerage_log/multi',
                    import_url: 'user/brokerage_log/import',
                    table: 'user_brokerage_log',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);

                $("input[name='from_user_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");

                $("input[name='to_user_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");

                $("input[name='order_id']", form)
                    .addClass("selectpage")
                    .data("source", "order/order/index")
                    .data("primaryKey", "id")
                    .data("field", "order_no")
                    .data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });


            function openOrderDetail(id){
                Fast.api.open('order/order/detail?ids='+id);
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                commonSearch: true,
                showExport: true,
                searchFormVisible: true,
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'mold', title: __('Mold'), searchList: {"order":__('Mold order'),"claim":__('Mold claim'),"exchange":__('Mold exchange')}, formatter: Table.api.formatter.normal},
                        {field: 'mold', title: __('Mold'), searchList: {"order":__('Mold order'),"exchange":__('Mold exchange')}, formatter: Table.api.formatter.normal},
                        {field: 'mold2', title: __('Mold2'), searchList: {"brokerage":__('Mold2 brokerage'),"score":__('Mold2 score')}, formatter: Table.api.formatter.normal},
                        // {field: 'from_user_group_id', title: __('From_user_group_id'), visible:false, operate: 'LIKE %...%'},
                        {field: 'fromusergroup.name', title: __('From_user_group_id'), operate: false},
                        {field: 'from_user_id', title: __('From_user_id'), visible:false, operate: 'LIKE %...%'},
                        {field: 'fromuser.nickname', title: __('From_user_id'), operate: false},
                        // {field: 'to_user_group_id', title: __('To_user_group_id'), visible:false, operate: 'LIKE %...%'},
                        {field: 'tousergroup.name', title: __('To_user_group_id'), operate: false},
                        {field: 'to_user_id', title: __('To_user_id'), visible:false, operate: 'LIKE %...%'},
                        {field: 'touser.nickname', title: __('To_user_id'), operate: false},
                        {field: 'order_id', title: __('Order_id'), visible:false, operate: 'LIKE %...%'},
                        {
                            field: 'orderid.order_no',
                            title: __('订单号'),
                            operate: false,
                            formatter: function (value,row,index) {
                                if(row.orderid) {
                                    return "<a target='_blank' class='btn btn-xs btn-primary btn-dialog' title='订单详情' data-tip='点击查看订单详情' href='"+row.order_jump_link+"'>"+row.orderid.order_no+"</a>";
                                }
                            }
                        },
                        // {field: 'claim_shop_id', title: __('Claim_shop_id'), visible:false, operate: 'LIKE %...%'},
                        // {field: 'claimshop.nickname', title: __('Claim_shop_id'), operate: false},
                        // {field: 'claimshop.shopname', title: __('Claim_shop_id'), operate: false},
                        // {field: 'user_coupon_exchange_id', title: __('User_coupon_exchange_id'), visible:false, operate: 'LIKE %...%'},
                        // {field: 'usercouponexchange.title', title: __('User_coupon_exchange_id'), operate: false},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'brokerage', title: __('Brokerage'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //当表格数据加载完成时 接收json传过来的附加值
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#total_amount").text(data.total_amount);
                $("#total_brokerage").text(data.total_brokerage);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});
